Chapter 12: Non-Parametric Tests
Welcome to the online content for Chapter 12!
As always, I’ll assume that you’ve already read up to this chapter of the book and worked through the online content for the previous chapters. If not, please do that first.
As always, click the ‘Run Code’ buttons below to execute the R code. Remember to wait until they say ‘Run Code’ before you press them. And be careful to run these boxes in order if later boxes depend on you having done other things previously.
Levene’s test
In Chapter 7, we used the variance ratio test to see if the variances of two samples were significantly different. Let’s read in that same data again:
I’ve used a variable called ‘set’ to distinguish each set of data (the ‘first’ set and the ‘second’ set).
To run the variance ratio test, we do:
This is identical to the \(F(24,19)=4.00, p=.003\), 95% CI [1.63,9.37] output that we obtained in Chapter 7.
To run Levene’s test, we need to install a package in R. Packages contain sets of functions that are useful for slightly more specialised purposes. We’ll use a package called ‘car’. The code to install it is:
You’ll see that there’s a pause (depending on your internet connection), while the package is installed. The option quiet=TRUE
avoids R telling us all the details about the installation and loading.
Once you’ve installed a package, you use the library
function to load it, so that it’s ready to use. This gives the message ‘Loading required package: carData’. The package will remain available now until you close the browser window.
Now we can run Levene’s test:
If we’d tried to use the leveneTest
function before installing the ‘car’ package, we’d have got an error (“could not find function”leveneTest”“).
For some reason, we have to use the as.factor(…)
function around ‘set’ to tell R that we’re treating set as a factor.
The output gives us an \(F\) value of 9.69 and \(p = .003\), which happens to be identical to the \(p\) value for the variance ratio test. We reject the null hypothesis of equal variances, and conclude that there’s heteroscedasticity.
Shapiro-Wilk test
First, let’s read in the data from Chapter 6 containing the 10 people’s heights.
We can repeat the 1-sample \(t\) test that we did there, with our null hypothesis that population mean (called ‘mu’) is 170 cm. The code was:
We obtained \(p=.129\), so we couldn’t reject the null hypothesis.
This test was valid only because the population was normally distributed. We can check that now, using the Shapiro-Wilk test:
The \(p\) value of .365 tells us that we can’t reject the null hypothesis that the data were normally distributed. This meant that running the \(t\) test was a valid thing to do.
Q-Q Plots
We can also test for normality by looking at the Q-Q plot.
This matches the output discussed in the chapter.
Skewed data
Now, let’s download the skewed data set used in the chapter:
Use the empty code box below to repeat the analyses with this data set.
Sign test
The easiest way to run the sign test, where there are 8 values above the median and 2 below, as in the chapter, is to do:
The name binom.test
is short for ‘Binomial test’, because the relevant statistical distribution is the Binomial distribution, but you don’t need to worry about that. The ‘10’ is the sample size, p=0.5
is saying that the null hypothesis is that the probability of being above the median (or below the median) is exactly 50%. The ‘8’ is the number of people above the median, but we’d get exactly the same result if we focused on the number of people below the median (2).
The \(p\) value is .109 whichever way we do it.
There are other functions for running the sign test, but, as explained in the chapter, it isn’t a very commonly-used test in practice, so I won’t bother with them.
Wilcoxon signed-rank test
Running this is just like running the 1-sample \(t\) test, which it’s analogous to.
For the \(t\) test, the code was t.test(people$height, mu=170)
.
For the Wilcoxon signed-rank test, we just replace t.test
with wilcox.test
.
We obtained \(p=.920\), so, as with the \(t\) test, we don’t reject the null hypothesis.
Let’s try it on the skewed data:
We get \(p=.084\), which matches the value discussed in the chapter.
Mann-Whitney U test
Let’s load in the data that we used in Chapter 6 for the independent-samples \(t\) test.
To run the Mann-Whitney test (also know as the Wilcoxon rank-sum test), we do:
The output matches the results discussed in the chapter.
Wilcoxon matched-pairs test
This time, we need to load in the data that we used in Chapter 6 for the paired-samples \(t\) test.
To run the Wilcoxon matched-pairs test, we do:
This gives us \(p=.002\), as discussed in the chapter.
Alternatives to classical ANOVA
Kruskal-Wallis test
We can run a Kruskal-Wallis test on the 1-way ANOVA data from Chapter 7.
Here are the data:
The code for running the ANOVA was:
For the Kruskal-Wallis, we do:
The \(p\) value matches the one given in the text.
Friedman test
Finally, in this section, we can run a Friedman test on the repeated-measures data from Chapter 7.
Here are the data:
The code for running the repeated-measures ANOVA was:
For the Friedman test, we do:
The \(p\) value matches the one given in the text.
Spearman’s rho and Kendall’s tau
In Chapter 9, we calculated Pearson’s \(r\) by using this code:
We can use method=…
to specify what kind of correlation we want:
With pearson
, we get the same value as above, because pearson
is the default used when no method is specified. All three values match those given in the text.
Using the rank function
Just for fun, let’s work out Spearman’s rho using Pearson. This is useful for reinforcing what Spearman’s rho is.
The rank
function converts each value into its rank. Let’s see what this does to height:
The first person’s height (165.32 cm) was the second shortest, so it gets a rank of 2.
The second person’s height (167.72 cm) was the sixth shortest, so it gets a rank of 6.
And so on.
Let’s rank the locations as well, and then correlate the two ranked variables:
Here, we’re using the cor
function without specifying a method, so by default we get the Pearson correlation of the ranks. This is the same as the Spearman correlation of the original values. The value 0.6867244 matches the Spearman correlation that we obtained above.